package com.inexas.cu.client.register;

import com.google.gwt.event.dom.client.*;
import com.google.gwt.user.client.ui.*;
import com.google.inject.*;
import com.inexas.cu.client.mvp.*;
import com.inexas.cu.client.mvp.Member.*;


public class SignInViewImpl extends Composite implements View<SignInPm> {
	private final Grid grid = new Grid(3, 2);
	private final TextBox accountNameTextBox = new TextBox();
	private final TextBox passwordTextBox = new PasswordTextBox();
	private final Button signInButton = new Button("Sign in");

	@Inject
	public SignInViewImpl() {
		grid.setText(0, 0, "Account name");
		grid.setWidget(0, 1, accountNameTextBox);

		grid.setText(1, 0, "Password");
		grid.setWidget(1, 1, passwordTextBox);

		grid.setWidget(2, 1, signInButton);
		initWidget(grid);
	}

	public void bind(final SignInPm pm) {
		accountNameTextBox.setText(pm.getAccountName().getValue());
		accountNameTextBox.addChangeHandler(new ChangeHandler() {
			public void onChange(ChangeEvent event) {
				pm.getAccountName().setValue(SignInViewImpl.this, accountNameTextBox.getText());
            }
			
		});
		
		passwordTextBox.setText(pm.getPassword().getValue());
		passwordTextBox.addChangeHandler(new ChangeHandler() {
			public void onChange(ChangeEvent event) {
				pm.getPassword().setValue(SignInViewImpl.this, passwordTextBox.getText());
            }
			
		});
		
		pm.getSignInActuator().addObserver(new Observer() {
			public void onChanged(Source source, Notification notification) {
				final boolean enabled = pm.getSignInActuator().getAccess() == Access.READWRITE;
				signInButton.setEnabled(enabled);
            }
		});
    }

}
